package com.ruoyi.xyq.config;

import com.ruoyi.xyq.domain.Withdrawal;
import com.ruoyi.xyq.domain.WithdrawalRequest;
import com.ruoyi.xyq.mapper.WithdrawalMapper;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class WithdrawalRollbackConsumer {

    @Autowired
    private WithdrawalMapper withdrawalMapper;

    @RabbitListener(queues = "withdrawal.dead.letter.queue")
    public void handleWithdrawalRollback(WithdrawalRequest request) {
        Withdrawal withdrawal = request.getWithdrawal();
        // 将提现金额回滚
        withdrawal.setCurrentBalance(withdrawal.getCurrentBalance() + request.getMoney());
        withdrawal.setCashWithdrawal(withdrawal.getCashWithdrawal() - request.getMoney());
        withdrawal.setStatus(1L); // 设置审核状态为失败（1）
        withdrawalMapper.updateWithdrawal(withdrawal);
    }
}